import { ItemMsgParams as Params } from '@swda/configs/params'


// 物品、武器、护甲、金钱、队友变更提示。
let SWDA_ChangeItemMsg = {};


// Change Gold
SWDA_ChangeItemMsg.command125 = Game_Interpreter.prototype.command125;
Game_Interpreter.prototype.command125 = function () {
    SWDA_ChangeItemMsg.command125.call(this);

    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 0);						// 类型：金钱
        $gameVariables.setValue(Params.OperationVarID, this._params[0]);	// 操作：增/减
        $gameVariables.setValue(Params.NumberVarID, this._params[2]);		// 数量

        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};

// Change Items
SWDA_ChangeItemMsg.command126 = Game_Interpreter.prototype.command126;
Game_Interpreter.prototype.command126 = function () {
    SWDA_ChangeItemMsg.command126.call(this);

    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 1);						// 类型：物品
        $gameVariables.setValue(Params.OperationVarID, this._params[1]);	// 操作：增/减
        $gameVariables.setValue(Params.ObjectVarID, this._params[0]);		// 对象：物品ID
        $gameVariables.setValue(Params.NumberVarID, this._params[3]);		// 数量

        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};

// Change Weapons
SWDA_ChangeItemMsg.command127 = Game_Interpreter.prototype.command127;
Game_Interpreter.prototype.command127 = function () {
    SWDA_ChangeItemMsg.command127.call(this);
    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 2);						// 类型：武器
        $gameVariables.setValue(Params.OperationVarID, this._params[1]);	// 操作：增/减
        $gameVariables.setValue(Params.ObjectVarID, this._params[0]);		// 对象：武器ID
        $gameVariables.setValue(Params.NumberVarID, this._params[3]);		// 数量
        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};

// Change Armors
SWDA_ChangeItemMsg.command128 = Game_Interpreter.prototype.command128;
Game_Interpreter.prototype.command128 = function () {
    SWDA_ChangeItemMsg.command128.call(this);
    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 3);						// 类型：护甲
        $gameVariables.setValue(Params.OperationVarID, this._params[1]);	// 操作：增/减
        $gameVariables.setValue(Params.ObjectVarID, this._params[0]);		// 对象：护甲ID
        $gameVariables.setValue(Params.NumberVarID, this._params[3]);		// 数量
        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};


// Change Party Member
SWDA_ChangeItemMsg.command129 = Game_Interpreter.prototype.command129;
Game_Interpreter.prototype.command129 = function () {
    SWDA_ChangeItemMsg.command129.call(this);
    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 4);						// 类型：队员
        $gameVariables.setValue(Params.OperationVarID, this._params[1]);	// 操作：入队/离队
        $gameVariables.setValue(Params.ObjectVarID, this._params[0]);		// 对象：角色ID
        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};

//【覆写】
Game_Party.prototype.addActor = function(actorId) {
    if (!this._actors.contains(actorId)) {
        this._actors.push(actorId);
        this.sortActors();
        $gamePlayer.refresh();
        $gameMap.requestRefresh();
    }
};
//【覆写】
Game_Party.prototype.removeActor = function(actorId) {
    if (this._actors.contains(actorId)) {
        this._actors.splice(this._actors.indexOf(actorId), 1);
        this.sortActors();
        $gamePlayer.refresh();
        $gameMap.requestRefresh();
    }
};
//队列变换后自动排序
Game_Party.prototype.sortActors = function() {
    return $gameParty._actors.sort(function(a,b){return a-b});
};

// Change Skill
SWDA_ChangeItemMsg.command318 = Game_Interpreter.prototype.command318;
Game_Interpreter.prototype.command318 = function() {
    SWDA_ChangeItemMsg.command318.call(this);
    if (Params.ShowMsg) {
        $gameVariables.setValue(Params.TypeVarID, 5);						// 类型：技能
        $gameVariables.setValue(Params.OperationVarID, this._params[2]);	// 操作：习得0/失去1
        if (this._params[0] === 0){                                         // 判断是用固定的角色ID还是用变量指定
            $gameVariables.setValue(Params.ObjectVarID, this._params[1]);	// 对象：角色ID（0为全体）
        }else{
            $gameVariables.setValue(Params.ObjectVarID, $gameVariables.value(this._params[1]));
        }
        $gameVariables.setValue(Params.NumberVarID, this._params[3]);		// 数量：这里是指技能ID
        var commonEvent = $dataCommonEvents[Params.CommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};



// 任务系统提示（YEP_QuestJournal）
SWDA_ChangeItemMsg.Game_Interpreter_pluginCommand =
    Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    SWDA_ChangeItemMsg.Game_Interpreter_pluginCommand.call(this, command, args);
    if (Params.ShowMsg) {
        if (command === 'Quest') {
            this.processQuestMsg(this.argsToString(args));
        }
    }
};


Game_Interpreter.prototype.processQuestMsg = function (line) {
    var showQuestMsg = true;

    if (this._index - 1 >= 0) {
        var lastEvent = this._list[this._index - 1];
        if (lastEvent.code === 356) {
            var lastCommand = lastEvent.parameters[0].split(" ").shift();
            if (lastCommand == 'Quest')
                showQuestMsg = false;
        }
    }
    if (showQuestMsg) {
        var commonEvent = $dataCommonEvents[Params.QuestCommonEventID];
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
};

//---------------------------
//          Game_Interpreter【插件指令】
//---------------------------
SWDA_ChangeItemMsg.PLUGIN_COMMAND = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
    SWDA_ChangeItemMsg.PLUGIN_COMMAND.call(this, command, args);


    if (command === 'ChangeItemMsg') {
        var ChangeItemMsgAction = args[0];
        if (ChangeItemMsgAction.toUpperCase() === "ON")
            Params.ShowMsg = true;
        if (ChangeItemMsgAction.toUpperCase() === "OFF")
            Params.ShowMsg = false;
    }
};
